console.log(Promise); //ƒ Promise() { [native code] }
pending
、fulfilled
、rejected
,狀態不會同時存在。then()
來接續後續的動作。catch()
來接續後續的動作。。 const a = new Promise(function (resolve, reject) { });
console.log(a);
const a = new Promise(function (resolve, reject) { return resolve('success') });
console.log(a); //Promise {<fulfilled>: 'success'}
a.then((param) => { console.log(param) }); //success,param只是用來接收fulfilled的
const a = new Promise(function (resolve, reject) { return reject('fail') });
console.log(a); //Promise {<rejected>: 'fail'}
a.catch((param) => { console.log(param) }); //fail,param只是用來接收rejected的
const a = function promiseFn(num) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve('real');
}
else {
reject('empty');
}
}, 10);
})
}
a(1)
.then((param) => console.log(param)); //real
function promiseFn(num) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve('real');
}
else {
reject('empty');
}
}, 10);
})
}
promiseFn(0)
.then((param) => console.log('sucess ' + param))
.catch((param) => console.log('false ' + param) //false empty
.then
跟.catch
可以合併撰寫,表示如下:
promiseFn(0)
.then((param) => { console.log('sucess ' + param) },
(param) => { console.log('false ' + param) }); //false empty
3.Promise chain:可以依序且依照不同結果,呼叫兩個以上的非同步函數。
function promiseFn(num) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) { //參數為真值時
resolve('real');
}
else { //參數為假值時
reject('empty');
}
}, 10);
})
}
promiseFn(0) //參數為假值,為reject狀態
.then((param) => {
console.log('sucess ' + param)
return promiseFn(1)
},
(param) => { //接收reject狀態時的參數
console.log('false ' + param)
return promiseFn(2) //回傳參數2給PromiseFn判斷,為真值,為fulfilled狀態
})
.then((param) => { //接收fulfilled狀態時的參數
console.log('sucess ' + param)
return promiseFn(0) //回傳參數0給PromiseFn判斷
},
(param) => {
console.log('false ' + param)
return promiseFn(1)
})
.then((param) => {
console.log('sucess ' + param)
},
(param) => {
console.log('false ' + param)
})
入門級Promise用法:
1.Promise.all
:會依據給定不同的時間點執行且執行結果為fulfilled後,並再傳給.then
後統一執行後續;如果途中有任一結果為rejected,就會直接找.catch
執行後續。
function promiseFn(num, time = 500) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve(`real ${num}`);
}
else {
reject('empty');
}
}, time);
});
}
Promise.all([
promiseFn(1, 1000),
promiseFn(2, 2000),
promiseFn(3, 3000),
])
.then((param) => { console.log(param) }) //(3) ['real 1', 'real 2', 'real 3']
.then((param) => { console.log(param[0], param[1], param[2])})//real 1 real 2 real 3
2.Promise.race
:會依據給定不同的時間點執行且執行結果為fulfilled後,且只會將第一個執行完畢的結果傳給.then
後執行後續;如果有任一執行結果為rejected,則依照次序決定
是否呈現fulfilled的後續結果或是呈現為rejected的後續結果。皆為fulfilled
function promiseFn(num, time = 500) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve(`real ${num}`);
}
else {
reject('empty');
}
}, time);
});
}
Promise.race([
promiseFn(1, 1000),
promiseFn(2, 20),
promiseFn(3, 3000),
])
.then((param) => { console.log(param) }) //real 2,因為只會呈現promiseFn(2, 20)
任一為rejected
function promiseFn(num, time = 500) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve(`real ${num}`);
}
else {
reject('empty');
}
}, time);
});
}
Promise.race([
promiseFn(0, 1000),
promiseFn(2, 20),
promiseFn(3, 3000),
])
.then((param) => { console.log(param) }) //real 2,promiseFn(2, 20)次序最前
.catch((param) => { console.log(param) })
function promiseFn(num, time = 500) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (num) {
resolve(`real ${num}`);
}
else {
reject(`empty ${num}`);
}
}, time);
});
}
Promise.race([
promiseFn(1, 1000),
promiseFn(0, 20),
promiseFn(3, 3000),
])
.then((param) => { console.log(param) })
.catch((param) => { console.log(param) }) //empty 0,因為promiseFn(0, 20)次序靠前